package com.chinaoct.siku.LocalStorage.controller;

import com.chinaoct.siku.LocalStorage.dao.StorageDTO;
import com.chinaoct.siku.LocalStorage.dao.StorageVO;
import com.chinaoct.siku.LocalStorage.service.StorageApplication;
import com.chinaoct.siku.LocalStorage.service.StorageService;
import com.chinaoct.siku.util.FileUtil;
import com.chinaoct.siku.util.ResponseMsg;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;

@Slf4j
@RestController
@RequestMapping(value = "/api/storage")
public class StorageController {

    @Autowired
    private StorageService storageService;

    @Autowired
    private StorageApplication storageApplication;

    /**
     * 显示单张图片
     * @return
     */
    @GetMapping(value = "/file/{fdId}",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ApiOperation("预览图片")
    public void showPhotos(@PathVariable String fdId,String thumbnail, HttpServletResponse response){

        if(StringUtils.isEmpty(fdId)){
            response.setStatus(401);
            return;
        }

        StorageDTO storageDTO = storageService.findByRealName(fdId);
        if(null == storageDTO){
            response.setStatus(401);
            return;
        }
        Boolean b = storageApplication.checkAuthority(storageDTO);
        if(!b){
            response.setStatus(401);
            return;
        }
        if(null!=storageDTO){
            File file =null;
            if(!StringUtils.isEmpty(thumbnail) && thumbnail.equals("1")){
                if(!StringUtils.isEmpty(storageDTO.getThumbnail())){
                    file = new File(storageDTO.getThumbnail());
                    if(file.exists()){
                        FileUtil.responseFileStream(response,file);
                        return;
                    }
                }
            }
            file = new File(storageDTO.getPath());
            if(file.exists()){
                FileUtil.responseFileStream(response,file);
                return;
            }else {
                response.setStatus(401);
                return;
            }
        }else {
            response.setStatus(401);
            return;
        }
    }

    @PostMapping(value = "uploadFile")
    @ApiOperation("上传文件")
    public ResponseMsg uploadFile(@RequestParam String name, @RequestParam String sourceType,  @RequestParam("file") MultipartFile file){

        try {
            StorageDTO storageDTO = storageService.saveFile(name,sourceType, file);
            StorageVO vo = storageService.changeToStorageVO(storageDTO);
            return new ResponseMsg(vo);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return ResponseMsg.error("-1",e.getMessage());
        }
    }
}
